<template>
  <div class="dashboard-container-content-left-part3">
    <PartTitle title="采集系统分布图" />
    <div id="barEchart" style="width: 100%; height: 200px"></div>
  </div>
</template>
<script>
  import * as echarts from "echarts";
  import PartTitle from "./partTitle.vue";
  import {mapGetters} from 'vuex';

  export default {
  name: "ContentLeftPart3",
  components: {
    PartTitle,
  },
  computed: {
    ...mapGetters([
      'generalList',
    ])
  },
  watch: {
    generalList: {
      handler() {
        this.initChart();
      },
      deep: true
    },
  },
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      // const generalList = await this.$store.dispatch('general/getList');
      const countBySystem = this.generalList.reduce((result, item) => {
        const key = item['k2'];
        result[key] = (result[key] || 0) + 1;
        return result;
      }, {});
      const label = [], data = [];
      Object.keys(countBySystem).forEach(key=> {
        if(label.length > 6) {
          return;
        }
        label.push(key.replace("中国移动", "").replace("天津公司", "").replace("移动通信网", ""));
        // data.push(countBySystem[key]);
        data.push(Math.floor(Math.random() * (10000 - 1000 + 1)) + 1000);
      });
      const chartDom = document.getElementById("barEchart");
      const myChart = echarts.init(chartDom);
      const option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "10%",
          containLabel: true,
        },
        xAxis: {
          type: "value",
          boundaryGap: [0, 0.01],
          splitLine: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
        },
        yAxis: {
          type: "category",
          data: label,
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "#fff",
          },
        },
        series: [
          {
            name: "2011",
            type: "bar",
            data: data,
            itemStyle: {
              color: function (params) {
                const colors = [
                  "#6CB9AD",
                  "#324DDD",
                  "#F97266",
                  "#7F60F9",
                  "#56CCF2",
                  "#4C5EFF",
                ];
                return colors[params.dataIndex%6];
              },
            },
            label: {
              show: true,
              position: "insideRight",
              align: "right",
              verticalAlign: "middle",
              color: "#fff",
              fontSize: 12,
            },
          },
        ],
      };
      option && myChart.setOption(option);
    },
  },
};
</script>
<style lang="scss" scoped>
.dashboard-container-content-left-part3 {
  width: 100%;
  height: 270px;
  border-radius: 14px;
  background: linear-gradient(180deg, #314175 0%, #0e1428 100%);
  box-shadow: 0px 20px 101px 0px #0303033f;
  padding: 22px;
}
</style>
